package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.questionbank.SingleChoiceQuestionStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 单选题实体类
 * 对应数据库表：sc_questions
 *
 * 根据B-02-03-03单选题管理子模块设计文档实现
 * 管理单选题的基础信息，包括题干内容、标题、难度级别、分类、状态等。
 * 支持单选题的创建、编辑、发布、归档等全生命周期管理。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "sc_questions")
@Comment("单选题基础信息表")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SingleChoiceQuestion {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 题目唯一标识符
     */
    @NotBlank(message = "题目ID不能为空")
    @Size(max = 50, message = "题目ID长度不能超过50个字符")
    @Column(name = "question_id", nullable = false, length = 50, unique = true)
    private String questionId;

    /**
     * 题目标题
     */
    @NotBlank(message = "题目标题不能为空")
    @Size(max = 200, message = "题目标题长度不能超过200个字符")
    @Column(name = "title", nullable = false, length = 200)
    private String title;

    /**
     * 题干内容
     */
    @NotBlank(message = "题干内容不能为空")
    @Column(name = "content", nullable = false, columnDefinition = "TEXT")
    private String content;

    /**
     * 正确答案索引（从1开始）
     */
    @NotNull(message = "正确答案不能为空")
    @Min(value = 1, message = "正确答案必须大于0")
    @Column(name = "correct_answer_index", nullable = false)
    private Integer correctAnswerIndex;

    /**
     * 解析说明
     */
    @Column(name = "explanation", columnDefinition = "TEXT")
    private String explanation;

    /**
     * 分类ID
     */
    @Column(name = "category_id")
    private Long categoryId;

    /**
     * 难度级别
     */
    @Min(value = 1, message = "难度级别必须大于0")
    @Max(value = 5, message = "难度级别不能超过5")
    @Column(name = "difficulty_level")
    private Integer difficultyLevel;

    /**
     * 知识点（JSON格式）
     */
    @Column(name = "knowledge_points", columnDefinition = "TEXT")
    private String knowledgePoints;

    /**
     * 标签（JSON格式）
     */
    @Column(name = "tags", columnDefinition = "TEXT")
    private String tags;

    /**
     * 预计答题时间（分钟）
     */
    @Min(value = 1, message = "预计答题时间必须大于0")
    @Column(name = "estimated_time_minutes")
    private Integer estimatedTimeMinutes;

    /**
     * 分值
     */
    @Min(value = 0, message = "分值不能为负数")
    @Column(name = "score", precision = 5, scale = 2)
    private BigDecimal score;

    /**
     * 版本号
     */
    @Min(value = 1, message = "版本号必须大于0")
    @Column(name = "version", nullable = false)
    private Integer version;

    /**
     * 题目状态
     */
    @NotNull(message = "题目状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private SingleChoiceQuestionStatus status;

    /**
     * 创建者ID
     */
    @NotNull(message = "创建者ID不能为空")
    @Column(name = "creator_id", nullable = false)
    private Long creatorId;

    /**
     * 创建者姓名
     */
    @Size(max = 50, message = "创建者姓名长度不能超过50个字符")
    @Column(name = "creator_name", nullable = false, length = 50)
    private String creatorName;

    /**
     * 审核者ID
     */
    @Column(name = "reviewer_id")
    private Long reviewerId;

    /**
     * 审核者姓名
     */
    @Size(max = 50, message = "审核者姓名长度不能超过50个字符")
    @Column(name = "reviewer_name", length = 50)
    private String reviewerName;

    /**
     * 审核时间
     */
    @Column(name = "review_time")
    private LocalDateTime reviewTime;

    /**
     * 审核意见
     */
    @Column(name = "review_comment", columnDefinition = "TEXT")
    private String reviewComment;

    /**
     * 使用次数
     */
    @Min(value = 0, message = "使用次数不能为负数")
    @Column(name = "usage_count", nullable = false)
    private Integer usageCount;

    /**
     * 正确率
     */
    @Column(name = "accuracy_rate", precision = 5, scale = 4)
    private BigDecimal accuracyRate;

    /**
     * 质量评分
     */
    @Min(value = 0, message = "质量评分不能为负数")
    @Max(value = 5, message = "质量评分不能超过5")
    @Column(name = "quality_score", precision = 3, scale = 2)
    private BigDecimal qualityScore;

    /**
     * 是否启用随机排序选项
     */
    @Column(name = "randomize_options", nullable = false)
    private Boolean randomizeOptions;

    /**
     * 软删除标识
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", nullable = false, length = 50)
    private String updatedBy;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

}